/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.serverpackets;

import java.io.IOException;
import java.util.ArrayList;

import l1j.opqlo.NewSystem.寄賣系統.AutoShop;
import l1j.opqlo.NewSystem.寄賣系統.AutoShopTable;
import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.templates.L1Item;

public class S_AutoShopSellList extends ServerBasePacket {

	/**
	 * 店の品物リストを表示する。キャラクターがBUYボタンを押した時に送る。
	 */
	public S_AutoShopSellList(final int objId) {
		this.writeC(S_OPCODE_SHOWSHOPBUYLIST);
		this.writeD(objId);

		final L1Object npcObj = L1World.getInstance().findObject(objId);
		if (!(npcObj instanceof L1NpcInstance)) {
			this.writeH(0);
			return;
		}
		// final int npcId = ((L1NpcInstance)
		// npcObj).getNpcTemplate().get_npcId();

		// final L1TaxCalculator calc = new L1TaxCalculator(npcId);
		// final L1Shop shop = ShopTable.getInstance().get(npcId);
		// final List<L1ShopItem> shopItems = shop.getSellingItems();
		final ArrayList<AutoShop> shoplist = AutoShopTable.getInstance()
				.getAutoShopList();
		// writeH(shopItems.size());
		this.writeH(shoplist.size());
		// L1ItemInstanceのgetStatusBytesを利用するため
		// final L1ItemInstance dummy = new L1ItemInstance();

		for (int i = 0; i < shoplist.size(); i++) {
			this.writeD(i);
			final AutoShop autoshop = shoplist.get(i);
			final L1ItemInstance item = autoshop.getIteminstance();
			this.writeH(autoshop.getGfxid());
			this.writeD(autoshop.getPrice());

			final long count = item.getCount();
			if (count > 1) {
				this.writeS(item.getNumberedViewName(count));
			} else {
				this.writeS(item.getLogName());

			}
			final L1Item template = ItemTable.getInstance().getTemplate(
					item.getItemId());
			if (template == null) {
				this.writeC(0);
			} else {
				// dummy.setItem(template);
				final byte[] status = item.getStatusBytes();
				this.writeC(status.length);
				for (final byte b : status) {
					this.writeC(b);
				}
			}
		}
		this.writeH(0x07); // 0x00:kaimo 0x01:pearl 0x07:adena
		/*
		 * for (int i = 0; i < shopItems.size(); i++) { final L1ShopItem
		 * shopItem = shopItems.get(i); final L1Item item = shopItem.getItem();
		 * final int price = calc.layTax((int) (shopItem.getPrice() *
		 * Config.RATE_SHOP_SELLING_PRICE)); writeD(i);
		 * writeH(shopItem.getItem().getGfxId()); writeD(price);
		 * 
		 * 
		 * if (shopItem.getPackCount() > 1) { writeS(item.getName() + " (" +
		 * shopItem.getPackCount() + ")"); } else { if
		 * (shopItem.getRemainingTime() >0){ writeS(item.getName()+"(限時:" +
		 * shopItem.getRemainingTime() + "秒)"); }else{ writeS(item.getName()); }
		 * } final L1Item template =
		 * ItemTable.getInstance().getTemplate(item.getItemId()); if (template
		 * == null) { writeC(0); } else { dummy.setItem(template); final byte[]
		 * status = dummy.getStatusBytes(); writeC(status.length); for (final
		 * byte b : status) { writeC(b); } } } writeH(0x07); // 0x00:kaimo
		 * 0x01:pearl 0x07:adena
		 */
	}

	@Override
	public byte[] getContent() throws IOException {
		return this._bao.toByteArray();
	}
}
